package com.itheima.controller;

import com.itheima.pojo.Clazz;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@Slf4j
@RequestMapping("/clazzs")
@RestController
public class ClazzController {

    @Autowired
    private ClazzService clazzService;

    //查询班级包括分页查询
    @GetMapping
    public Result clazzPage(
            @RequestParam(required = false) String name,           // 班级名称（可选）
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,  // 范围匹配的开始日期（可选）
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end,    // 范围匹配的结束日期（可选）
            @RequestParam(defaultValue = "1") Integer page,        // 当前页，默认第1页
            @RequestParam(defaultValue = "10") Integer pageSize     // 每页条数，默认10条
    ) {
        // 将参数传递给 Service 层
        PageResult<Clazz> pageResult = clazzService.clazzPage(name, begin, end, page, pageSize);
        return Result.success(pageResult);
    }

    @PostMapping
    public Result add(@RequestBody Clazz clazz) {
        log.info("新增班级：{}", clazz);
        clazzService.add(clazz);
        return Result.success();
    }


    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Integer id) {
        //log.info("根据 id 查询班级信息，id：{}",id);
        Clazz clazz = clazzService.getInfo(id);
        return Result.success(clazz);
    }


    @PutMapping
    public Result update(@RequestBody Clazz clazz) {
        log.info("修改班级信息：{}", clazz);
        clazzService.update(clazz);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        log.info("根据 id 删除班级信息，id：{}", id);
        clazzService.delete(id);
        return Result.success();
    }

    @GetMapping("/list")
    public Result list() {
        log.info("查询所有班级信息");
        List<Clazz> clazzList = clazzService.list();
        return Result.success(clazzList);
    }

}
